package Browser;

import java.awt.BorderLayout;
import java.sql.SQLException;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import javax.swing.JApplet;
import javax.swing.JFrame;

import crawler.src.Filter;

public class Browser extends JApplet{
	
	 private static final int PANEL_WIDTH_INT = 675;
     private static final int PANEL_HEIGHT_INT = 400;
     private static final int TABLE_PANEL_HEIGHT_INT = 100;
     private static JFXPanel browserFxPanel;
     private Pane browser;
     private Filter filter;
	
     public Browser(){
    	 try {
			filter = new Filter();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     }
     
     public void init() {

    	 JFrame browser = new JFrame("Browler");
         // create javafx panel for browser
         browserFxPanel = new JFXPanel();
         
         browserFxPanel.setLayout(new BorderLayout());
         
         add(browserFxPanel, BorderLayout.CENTER);
         
         // create JavaFX scene
         Platform.runLater(new Runnable() {
             public void run() {
                 createScene();
             }
         });
     }
     
	 private Pane createBrowser() {
		 
		System.setProperty("jsse.enableSNIExtension", "false");
	 
        Double widthDouble = new Integer(PANEL_WIDTH_INT).doubleValue();
        Double heightDouble = new Integer(PANEL_HEIGHT_INT).doubleValue();
        WebView view = new WebView();
        view.setMinSize(widthDouble, heightDouble);
        view.setPrefSize(widthDouble, heightDouble);
        final WebEngine eng = view.getEngine();
        final Label warningLabel = new Label("Do you need to specify web proxy information?");
        eng.load("http://www.google.com/");

        ChangeListener handler = new ChangeListener<Number>() {
            @Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (warningLabel.isVisible()) {
                    warningLabel.setVisible(false);
                }
            }
        };
        eng.getLoadWorker().progressProperty().addListener(handler);

        final TextField locationField = new TextField("http://www.google.com/");
        locationField.setMaxHeight(Double.MAX_VALUE);
        
        Button goButton = new Button("Go");
        goButton.setDefaultButton(true);
        EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
            	try {
            		String url = new String(locationField.getText().startsWith("http") ? locationField.getText() : "http://" + locationField.getText());
            		
            		if (Definitions.getSecurityItem().equals("None")) {
            			eng.load(filter.translateUrl(url));
            		}
            		else {
            			filter.setUrl(url);
            			filter.execute();
	            		if(!filter.blocked(url))
	            			eng.load(url);
	            		else
	            			eng.loadContent("<html><h1><center>Blocked Content</center></h1><br><br>"
	            					+ "<br><p><center>This page has been blocked due to containing "
	            					+ "inappropriate content</center></p> </html>");	  
            		}
            	}catch(Exception e) {
            		System.out.println("ERRO NO FILTRO!");
            	}
                
            }
        };
        goButton.setOnAction(goAction);
        goButton.setStyle("-fx-font: 12 arial; -fx-base: #4F94CD;");
        locationField.setOnAction(goAction);
        eng.locationProperty().addListener(new ChangeListener<String>() {
            @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                locationField.setText(newValue);
            }
        });
        
        Button defButton = new Button("Definitions");
        defButton.setStyle("-fx-font: 12 arial; -fx-base: #999999;");
        defButton.setDefaultButton(true);
        EventHandler<ActionEvent> defAction = new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
            		Password password = new Password();
            	}
        };
        defButton.setOnAction(defAction);
        
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(5));
        grid.setVgap(5);
        grid.setHgap(5);
        GridPane.setConstraints(locationField, 0, 0, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
        GridPane.setConstraints(goButton, 2, 0);
        GridPane.setHalignment(goButton, HPos.LEFT);
        GridPane.setConstraints(defButton, 2, 0);
        GridPane.setHalignment(defButton, HPos.RIGHT);
        GridPane.setConstraints(view, 0, 1, 3, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
        GridPane.setConstraints(warningLabel, 0, 2, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
        grid.getColumnConstraints().addAll(
                new ColumnConstraints(widthDouble - 200, widthDouble - 200, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
                new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
        grid.getChildren().addAll(locationField, goButton, defButton, warningLabel, view);
        return grid;
	 }
	 
	 
	 private void createScene() {
	        browser = createBrowser();
	        browserFxPanel.setScene(new Scene(browser));
	    }
}
